﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoreData;
using EntityData;
using Helper;
using System.Web.Mvc;
namespace Services
{

    public partial class MenuService
    {
       readonly IMenuEntity entity;

       public MenuService(IMenuEntity entity)
       {
           this.entity = entity;

       }

       #region Main Method

       /// <summary>
       /// Save 
       /// </summary>
       /// <param name="_model"></param>
       /// <returns></returns>
       public long Save(object _model)
       {
           try
           {
               var obj = ((Menu)_model);
               var id = obj.ID;
               if (id == 0)
               {

                   obj.Code = obj.Name;
                  
                   if (obj.Image == null || obj.Image == "")
                   {
                       obj.Image = "noimage.png";
                   }
                   return entity.Save((CoreData.Menu)_model, Table.Menu.ToString());
               }
               else
               {
                   if (obj.Image == null || obj.Image == "")
                   {
                       obj.Image = GetByID(obj.ID).Image;
                   }

                   return entity.Update((CoreData.Menu)_model, Table.Menu.ToString());
               }
           }
           catch { return -1; }
       }

       /// <summary>
       /// Get by ID
       /// </summary>
       /// <param name="_id"></param>
       /// <returns></returns>
       public CoreData.Menu GetByID(long _id)
       {
           try
           {
               return entity.GetById(_id, Table.Menu.ToString());
           }
           catch { return null; }
       }

       /// <summary>
       /// Get List
       /// </summary>
       /// <returns></returns>
       public IList<CoreData.Menu> GetList()
       {
           try
           {
               return entity.GetAll(Table.Menu.ToString()).ToList();
           }
           catch { return null; }

       }

       /// <summary>
       /// Get List with Linq
       /// </summary>
       /// <returns></returns>
       public IList<CoreData.Menu> GetListByLINQ(Func<CoreData.Menu, Boolean> _where)
       {
           try
           {
               return entity.GetMany(_where, Table.Menu.ToString()).ToList();
           }
           catch { return null; }
       }
       /// <summary>
       /// Get One with Linq
       /// </summary>
       /// <returns></returns>
       public CoreData.Menu GetOneByLINQ(Func<CoreData.Menu, Boolean> _where)
       {
           try
           {
               return entity.Get(_where, Table.Menu.ToString());
           }
           catch { return null; }
       }

       /// <summary>
       /// Get List with search string
       /// </summary>
       /// <returns></returns>
       public IList<CoreData.Menu> GetList(string _searchstring)
       {
           //search theo tieu chi nao do         
           try
           {
               return entity.GetBySearchString(_searchstring, Table.Menu.ToString()).ToList();
           }
           catch { return null; }
       }


       /// <summary>
       /// Delete
       /// </summary>
       /// <param name="_model"></param>
       /// <returns></returns>
       public bool Delete(object _model)
       {
           try
           {
               entity.Delete((CoreData.Menu)_model, Table.Menu.ToString());
               return true;
           }
           catch { return false; }
       }


       #endregion



        #region Other Method
       public List<SelectListItem> GetListPosition(int id)
       {
           List<SelectListItem> enumList = new List<SelectListItem>();
           foreach (ValueDefine.MenuPosition data in Enum.GetValues(typeof(ValueDefine.MenuPosition)))
           {
               SelectListItem sl = new SelectListItem();
               sl.Text = data.ToString().Replace("_", " ");
               sl.Value = ((int)Enum.Parse(typeof(ValueDefine.MenuPosition), data.ToString())).ToString();
               if ((int)data == id) {
                   sl.Selected = true;
               }
               enumList.Add(sl);
           }
           return enumList;
       }
       public List<SelectListItem> GetListParent(int id)
       {
           List<SelectListItem> lst = new List<SelectListItem>();
           lst.Add(new SelectListItem() {
                Selected =true,
                Text ="--------",
                Value="0"
           });
           var data = GetListByLINQ(c=>c.ParentID==0);
           foreach (var item in data)
           {
               SelectListItem _item = new SelectListItem();
               _item.Text = item.Name;
               _item.Value = item.ID.ToString();
               if (id == item.ID)
               {
                   _item.Selected = true;
               }
               lst.Add(_item);
           }
           return lst;
       }

        /// <summary>
        /// Get menu show lên header trang chủ
        /// </summary>
        /// <returns></returns>
       public List<Menu> GetMenuOnHome()
       {
           try
           {
               var data = GetListByLINQ(c => c.Active == true & c.ParentID==0);
               foreach (var item in data)
               {
                   var sub = GetListByLINQ(c => c.ParentID==item.ID);
                   if (sub != null&&sub.Count!=0)
                   {
                       item.SubMenuExt = sub.ToList();
                   }
               }
               return data.ToList() ;
           }
           catch { return new List<Menu>(); }
       }

        #endregion 
    
    }
         
}
